package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityForecastData;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.BatchForecastOrderRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.controller.MachDishesController;
import com.ocom.payhandle.dao.IDaoForecastData;
import com.ocom.payhandle.dao.IDaoPersonnelInfo;
import com.ocom.payhandle.feign.PayConfigClientService;
import com.ocom.payhandle.service.ForecastDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ForecastDataServiceImpl extends ServiceImpl<IDaoForecastData, EntityForecastData> implements ForecastDataService {


    @Autowired
    private IDaoForecastData daoForecastData;

    @Autowired
    private PayConfigClientService payConfigClientService;


    @Autowired
    private MachDishesController machDishesController;


    @Autowired
    private IDaoPersonnelInfo daoPersonnelInfo;

    //有bug ！ 2025年9月18日13:33:15
    @Deprecated
    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.SLAVE)
    public Result batchForecastOrder(Long companyId, BatchForecastOrderRequest request) {
//        request.setMealDate(DateUtil.endOfDay(request.getMealDate()));
//        log.info("定时任务开始批量订餐：参数：【{}】",JSON.toJSONString(request));
//        List<EntityForecastData> forecastDataList = this.list(Wrappers.lambdaQuery(EntityForecastData.class)
////                .eq(EntityForecastData::getComId,request.getComId())
//                .in(EntityForecastData::getStatus,1,3)
//                .apply("  {0} between  DATE_ADD(meal_date,INTERVAL (-allow_days *24 +allow_time) HOUR ) and meal_date",request.getMealDate())
//                .orderByDesc(EntityForecastData::getMealDate,EntityForecastData::getMealNo)
//        );
//        log.info("定时任务批量订餐：查询到的待订餐的订单：【{}】",JSON.toJSONString(forecastDataList));
//        forecastDataList.forEach(entityForecastData -> {
//            GetOrderPlanDishesRequest getOrderPlanDishesRequest = new GetOrderPlanDishesRequest();
//            getOrderPlanDishesRequest.setComId(entityForecastData.getComId());
//            getOrderPlanDishesRequest.setMealNo(Integer.valueOf(entityForecastData.getMealNo()));
//            getOrderPlanDishesRequest.setMealDate(DateUtil.formatDate(entityForecastData.getMealDate()));
//            getOrderPlanDishesRequest.setPointId(entityForecastData.getPointId());
//            Result result = payConfigClientService.getOrderPlanDishes(getOrderPlanDishesRequest);
//            if(result.getCode() == 200){
//                String json = JSON.toJSONString(result.getData());
//                List<GetOrderPlanDishesResponse> getOrderPlanDishesResponseList = JSON.parseArray(json,GetOrderPlanDishesResponse.class);
//                if(ObjectUtil.isNotEmpty(getOrderPlanDishesResponseList)){
//                    GetOrderPlanDishesResponse getOrderPlanDishesResponse = getOrderPlanDishesResponseList.get(0);
//                    AtomicReference<GetOrderPlanDishesResponse.Price> price = new AtomicReference<>();
//                    //查询单价
//                    getOrderPlanDishesResponse.getPriceList().forEach(p -> {
//                        if(p.getPriceMode().equals("20") ){
//                            price.set(p);
//                        }
//                    });
//                    if(ObjectUtil.isEmpty(price.get())){
//                        return;
//                    }
//                    QueryWrapper<?> queryWrapper = new QueryWrapper<>();
//                    queryWrapper.eq(" p.com_id",entityForecastData.getComId()).eq(" p.status",0).eq("p.p_id",entityForecastData.getPId()).orderByDesc("p.p_id").last("limit 1");
//
//                    PersonAndDeptVo personAndDept = daoPersonnelInfo.selectPerson(queryWrapper);
//                    if(ObjectUtil.isEmpty(personAndDept)){
//                        return ;
//                    }
//
//                    OrderMealPlanSubmitRequest orderMealPlanSubmitRequest = new OrderMealPlanSubmitRequest();
//                    orderMealPlanSubmitRequest.setMealDate(DateUtil.formatDate(entityForecastData.getMealDate()));
//                    orderMealPlanSubmitRequest.setMealNo(Integer.valueOf(entityForecastData.getMealNo()));
//                    orderMealPlanSubmitRequest.setComId(entityForecastData.getComId());
//                    orderMealPlanSubmitRequest.setMealPrice(Integer.valueOf(price.get().getDishesPrice()));
//                    orderMealPlanSubmitRequest.setPointId(entityForecastData.getPointId());
//                    orderMealPlanSubmitRequest.setReceiptName(personAndDept.getDeptName());
//                    orderMealPlanSubmitRequest.setReceiptId(personAndDept.getDeptId());
//
//                    List<DishesInfoVo> dishesInfoVoList = new ArrayList<>();
//                    DishesInfoVo dishesInfoVo = new DishesInfoVo();
//                    dishesInfoVo.setDishesId(getOrderPlanDishesResponse.getDishesId());
//                    dishesInfoVo.setDishesName(getOrderPlanDishesResponse.getDishesName());
//                    dishesInfoVo.setUnitPrice(Integer.valueOf(price.get().getDishesPrice()));
//                    dishesInfoVo.setPriceMode(Integer.valueOf(price.get().getPriceMode()));
//                    dishesInfoVo.setMealMenuId(getOrderPlanDishesResponse.getMealMenuId());
//                    dishesInfoVo.setWeightNumber(entityForecastData.getNumber());
//                    dishesInfoVo.setDishesPrice(entityForecastData.getNumber()*Integer.parseInt(price.get().getDishesPrice()));
//                    dishesInfoVoList.add(dishesInfoVo);
//                    orderMealPlanSubmitRequest.setMealMenuInfo(dishesInfoVoList);
//
//                    //计算餐段时间
//                    if(orderMealPlanSubmitRequest.getMealDate().equals(DateUtils.getDate())){
//                        ConsTimeConfigOnShowRequest consTimeConfigOnShowRequest =new ConsTimeConfigOnShowRequest();
//                        consTimeConfigOnShowRequest.setComId(entityForecastData.getComId());
//                        consTimeConfigOnShowRequest.setPage(1);
//                        consTimeConfigOnShowRequest.setSize(50);
//                        Result result1 = payConfigClientService.GetMealTimeInfo(consTimeConfigOnShowRequest);
//                        if (result1.getCode() == 200) {
//                            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(result1.getData()));
//
//                            List<GetMealTimeDto> list = JSON.parseArray(JSON.toJSONString(jsonObject.get("list")), GetMealTimeDto.class);
//
//                            for (GetMealTimeDto mealTimeDto : list) {
//                                if (ObjectUtil.isNotEmpty(orderMealPlanSubmitRequest.getMealNo()) && mealTimeDto.getMealNo().equals(orderMealPlanSubmitRequest.getMealNo().toString())) {
//                                    long nowLong = new Date().getTime();
//
//                                    //餐段时间
//                                    Date after = null;
//                                    try {
//                                        after = DateUtils.getDate(DateUtils.getDate() + " " + mealTimeDto.getBeginTime(), "yyyy-MM-dd HH:mm:ss");
//                                    } catch (ParseException e) {
//                                        continue;
//                                    }
//                                    long afterLong = after.getTime() - (mealTimeDto.getBeforeHour() * 3600000);
//
//                                    if (nowLong > afterLong) {
//                                        return ;
//                                    }
//                                }
//                                continue;
//                            }
//                        }
//                    }
//
//
//                    OrderDishesPayRequest orderDishesPayRequest =new OrderDishesPayRequest();
//
//                    String ord = RandomUtil.generateNumber(8);
//                    Date date= new Date();
//                    String orderTime = DateUtils.getStrDate(date,DateUtils.YYYYMMDDHHMMSS) ;
//                    String orderNo2 = ord+orderTime;
//                    orderNo2 = "DC" + NumConvertUtil.IntToHexStringNum(entityForecastData.getComId().intValue(),6) + orderNo2 ;
//
//                    orderDishesPayRequest.setComId(entityForecastData.getComId());
//                    orderDishesPayRequest.setMachNo("00000001");
//                    orderDishesPayRequest.setTradeNo(orderNo2);
//                    orderDishesPayRequest.setAmount(orderMealPlanSubmitRequest.getMealPrice());
//                    orderDishesPayRequest.setType(orderMealPlanSubmitRequest.getType());
//                    orderDishesPayRequest.setConsType(1);
//                    orderDishesPayRequest.setOpenId("");
//                    orderDishesPayRequest.setPId(Long.valueOf(entityForecastData.getPId()));
//                    orderDishesPayRequest.setConsState(5);
//                    orderDishesPayRequest.setPayWay(7);
//                    orderDishesPayRequest.setType(7);
//                    orderDishesPayRequest.setPointId(orderMealPlanSubmitRequest.getPointId() != null ? orderMealPlanSubmitRequest.getPointId() : 0);
//                    orderDishesPayRequest.setReceiptId(orderMealPlanSubmitRequest.getReceiptId());
//                    orderDishesPayRequest.setReceiptName(orderMealPlanSubmitRequest.getReceiptName());
//                    orderDishesPayRequest.setMealDate(orderMealPlanSubmitRequest.getMealDate());
//                    orderDishesPayRequest.setMealNo(orderMealPlanSubmitRequest.getMealNo());
//                    orderDishesPayRequest.setDishesInfo(orderMealPlanSubmitRequest.getMealMenuInfo());
//                    orderDishesPayRequest.setRemark(orderMealPlanSubmitRequest.getRemark());
//                    orderDishesPayRequest.setReceiptPrice(orderMealPlanSubmitRequest.getReceiptPrice());
//                    log.info("发起预订餐请求 jijijkl   提交点餐 【{}】原始参数 【{}】",orderDishesPayRequest,orderMealPlanSubmitRequest);
//                    orderDishesPayRequest.setReceiptPrice(orderMealPlanSubmitRequest.getReceiptPrice());
//                    Result resultA = machDishesController.orderDishesPay(orderDishesPayRequest);
//                    if(resultA.getCode() == 200){
//                        entityForecastData.setOrderNo(orderNo2);
//                        entityForecastData.setStatus(2);
//                        this.updateById(entityForecastData);
//                    }else {
//                        entityForecastData.setStatus(3);
//                        this.updateById(entityForecastData);
//                    }
//                }
//            }
//        });
        return ResultUtil.success();
    }
}
